package lab02.test24;

import java.util.Calendar;
import java.util.Scanner;

/**
 * Created by Administrator on 2017/11/1.
 */
public class Test24 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入计算的开始日期.");
        String beginDate = sc.nextLine();
        System.out.println("请输入计算的结束日期.");
        String endDate = sc.nextLine();

        String beginYear = beginDate.substring(0,4);
        String beginMonth = beginDate.substring(4,6);
        String beginDay = beginDate.substring(6,8);      //提取表示开始日期年月日的字符串

        int beginY = Integer.parseInt(beginYear);
        int beginM = Integer.parseInt(beginMonth);
        int beginD = Integer.parseInt(beginDay);         //将表示开始日期的字符串的年月日转换为int型

        String endYear = endDate.substring(0,4);
        String endMonth = endDate.substring(4,6);
        String endDay = endDate.substring(6,8);       //提取表示结束日期年月日的字符串

        int endY = Integer.parseInt(endYear);
        int endM = Integer.parseInt(endMonth);
        int endD = Integer.parseInt(endDay);           //将表示结束日期的字符串的年月日转换为int型

        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        calendar1.set(beginY,beginM ,beginD );           //设置开始日期
        calendar2.set(endY, endM, endD);           //设置结束日期

        long milliseconds1 = calendar1.getTimeInMillis();
        long milliseconds2 = calendar2.getTimeInMillis();  //分别返回开始时间的毫秒数和结束时间的毫秒数

        long days = (milliseconds2 - milliseconds1) / (24 * 60 * 60 * 1000);  //long型的整数 表示从1790-1-1 00:00:00到当前时间总共经过的时间的毫秒数。
        // 除以1000/60/60/24得到两个时间相差的天数。
        System.out.println("这两个日期相差" + days + "天");
    }
}

